#ifndef MC_SHADER_H
#define MC_SHADER_H

class MeshColorShader : public Shader 
{
public:
	MeshColorShader(): Shader() {}
	MeshColorShader(const char * vp_source, const char * fp_source)
		:Shader(vp_source, fp_source) {}

	inline void get_tex_coord (Point p[3],  float & k, float & h, float & c)
	{
		//compute c & k ;
		float a = (p[1]-p[2]).length(); 
		float b = (p[2]-p[0]).length();
		c = (p[0]-p[1]).length();
		k = (b*b + c * c - a * a) / (2.0f * c) ; 

		//compute h ;
		float cos_a = (b*b + c* c - a* a)/ (2.0f * b * c) ;
		float sin_a = sqrt (1.0f - cos_a * cos_a) ;
		h = b * sin_a ;

		//computer c ;

		if (h >= c) {k /= h ; c /= h; h = 1.0f;}
		else {k /=c; h/= c; c = 1.0f;}
	}
	inline void set_triangle_shader_uniform (Point point[3], GLfloat color [40], int resolution, 
		float k, float h, float c)
	{

		set_uniform("k", k); 
		set_uniform("h", h);
		set_uniform("c", c);
		set_uniform("res", resolution);
		set_uniform("color", color, (resolution + 1 ) * (resolution + 2) / 2);

	}
	inline void enable_draw_face () const {set_uniform("draw_face", 1); }
	inline void disable_draw_face () const {set_uniform("draw_face", 0); }


public:
	inline GLfloat compute_k(Point p[3]) const 
	{
		float a = (p[1]-p[2]).length(); 
		float b = (p[2]-p[0]).length();
		float c = (p[0]-p[1]).length();
		float k = (b*b + c * c - a * a) / (2.0f * c) ; 
		return GLfloat (k); 
	}
	inline GLfloat compute_h(Point p[3]) const
	{
		float a = (p[1]-p[2]).length(); 
		float b = (p[2]-p[0]).length();
		float c = (p[0]-p[1]).length();
		float cos_a = (b*b + c* c - a* a)/ (2.0f * b * c) ;
		float sin_a = sqrt (1.0f - cos_a * cos_a) ;
		return (b * sin_a) ;
	}
	inline GLfloat compute_c(Point p[3]) const
	{
		return (p[0] - p[1]).length();
	}
};

#endif
